package com.nowcoder.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author by lvshh
 * @version 1.0
 * @date 2020/4/25 15:00
 */
public class CookieUtil {

    public static String getValue(HttpServletRequest request,String name){
        //如果request或者cookie的key为空
        if (request == null || name == null){
            throw new IllegalArgumentException("参数为空");
        }
        //获取所有的cookie
        Cookie[] cookies = request.getCookies();
        //如果cookie集合不为空
        if (cookies != null){
            for (Cookie cookie : cookies) {
                //遍历cookies，将cookie的key与name进行匹配
                if (cookie.getName().equals(name)){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

}
